package com.situ.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.util.JDBCUtil;

@WebServlet("/StudentDelete")
public class StudentDeleteServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// http://localhost:8080/Java202007Web/studentDelete?id=23
		// 从浏览器传递过来的数据即使是数值类型得到的也是字符串类型
		String id = req.getParameter("id");
//		System.out.println(id);
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM student WHERE id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, Integer.parseInt(id));
			int count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count:" + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
		
		// 删除完了之后重定向到查找所有界面 /studentSelect
		// req.getContextPath()得到该项目的名字
		resp.sendRedirect(req.getContextPath() + "/StudentSelect");
	}

}
